package com.sdk.pay.transfer.utils;

import android.content.Context;

import com.sdk.pay.transfer.MainActivity;
import com.sdk.pay.transfer.api.PostQrCodeListAPI;
import com.sdk.pay.transfer.listener.UploadStatusListener;
import com.sdk.pay.transfer.model.WECHATBean;
import com.sdk.pay.transfer.provider.ProviderHelper;

import java.util.List;


/**
 * Created by Joe on 2017/09/29 13:44.
 */

public class UploadDataUtil {
    private Context mContext;
    private boolean isUploading = false;
    private static UploadDataUtil instance;
    private UploadStatusListener mUploadStatusListener;
    private int count = 0;

    public static UploadDataUtil getInstance() {
        if (instance == null) {
            instance = new UploadDataUtil();
        }
        return instance;
    }

    /**
     * Entrance
     */
    public void uploadDatasInOrder(Context context) {
        if (isUploading) {
            return;
        }
        isUploading = true;
        if (mUploadStatusListener != null) {
            mUploadStatusListener.uploadStatus(false);
        }
        mContext = context;
        todoUploadOFFICE();
    }

    /**
     * do upload or upload finish
     */
    private void todoUploadOFFICE() {
        List<WECHATBean> beanList = ProviderHelper.get_List_Top_Without_Report(mContext);
        if (beanList != null && beanList.size() > 0) {
            upload(beanList);
        } else {
            uploadSuccess();
        }
    }

    /**
     * do upload
     */
    private void upload(final List<WECHATBean> list) {
        if (list == null || list.size() < 1) {
            uploadSuccess();
            return;
        }
        LogUtil.log("beanList size = " + list.size());
        new PostQrCodeListAPI().postQrCodeList(list, new PostQrCodeListAPI.Listener() {
            @Override
            public void onSuccess(String message) {
                updateUploadDatas(list);
                count += list.size();
                MainActivity.instance.log("已上传：" + count);
                todoUploadOFFICE();
            }

            @Override
            public void onFailure(int code, String message) {
                isUploading = false;
                if (mUploadStatusListener != null) {
                    mUploadStatusListener.uploadStatus(true);
                }
                MainActivity.instance.log("onFailure code " + code + " |message " + message);
                MainActivity.instance.log("data upload failure ,wait for next upload");
            }
        });
    }

    /**
     * upload data  update db state
     */
    private void updateUploadDatas(List<WECHATBean> list) {
        for (WECHATBean wechatBean : list) {
            ProviderHelper.update(wechatBean.id, "2", mContext);
        }
    }

    /**
     * upload data with codeurl success
     * determine whether exist untreated data
     */
    private void uploadSuccess() {
        isUploading = false;
        count = 0;
        if (mUploadStatusListener != null) {
            mUploadStatusListener.uploadStatus(true);
        }
        List<WECHATBean> beanList = ProviderHelper.get_ListBean_Without_CodeUrl(mContext);
        if (beanList == null || beanList.size() < 1) {
            MainActivity.instance.log("upload success!");
            ProviderHelper.deleteWechat(mContext);
        } else {
            MainActivity.instance.log("exist data without codeUrl");
            ActivityHelper.startQRActivity();
            MainActivity.instance.onContinue();
        }
    }

    public void setUploadStatusListener(UploadStatusListener uploadStatusListener) {
        mUploadStatusListener = uploadStatusListener;
    }
}